#include "mediaitemimpl.h"
#include "tools.h"

#include <QFileInfo>
#include <QDir>
#include <QFile>
#include <QDateTime>
#include <QVariant>
#include <QDomElement>
#include <QImageReader>
#include <QProcess>
#include <QReadLocker>
#include <iostream>
#include <iomanip>
#include <exception>

MediaItemImpl::MediaItemImpl()
    : m_selected(false), m_checked(false)
{
}

MediaItemImpl::MediaItemImpl(const Metadata & metadata)
    : m_selected(false), m_checked(false)
{
    m_metadata = metadata;
}

MediaItemImpl::~MediaItemImpl()
{
}

void MediaItemImpl::load(QDomElement &e)
{
    m_metadata.collection  = e.attribute("collection");
    m_metadata.filename    = e.attribute("filename");
    m_metadata.modified    = QDateTime::fromString(e.attribute("modified"), Qt::ISODate);
    m_metadata.size        = e.attribute("size").toLong();
    m_metadata.isRaw       = (e.attribute("raw").toInt() == 1);
    m_metadata.created     = QDateTime::fromString(e.attribute("created"), Qt::ISODate);
    m_metadata.keywords    = QSet<QString>::fromList(e.attribute("keywords").split(',', QString::SkipEmptyParts));
}

void MediaItemImpl::save(QDomElement & e)
{
    e.setAttribute("collection", m_metadata.collection);
    e.setAttribute("filename",   m_metadata.filename);
    e.setAttribute("modified",   m_metadata.modified.toString(Qt::ISODate));
    e.setAttribute("size",       m_metadata.size);
    e.setAttribute("raw",        m_metadata.isRaw ? 1 : 0);
    e.setAttribute("created",    m_metadata.created.toString(Qt::ISODate));
    e.setAttribute("keywords",   QStringList(m_metadata.keywords.toList()).join(","));
}

//void MediaItemImpl::update()
//{
//    if(updateinternal())
//        m_database->update(m_database->item(filename()));
//}

//bool MediaItemImpl::move(const QString & collection, const QString &filename)
//{
//    QWriteLocker l(&m_lock);
//    if(QFile::exists(filename) || !QFile::exists(m_filename))
//        return false;
//
//    QFileInfo fi(filename);
//    QDir dir;
//    dir.mkpath(fi.absolutePath());
//
//    if(!QFile::rename(m_filename, filename))
//        return false;
//
//    m_collection = collection;
//    m_filename   = filename;
//    return true;
//}

void MediaItemImpl::setSelected(bool selected)
{
    m_selected = selected;
}

void MediaItemImpl::setChecked(bool checked)
{
    m_checked = checked;
}

bool MediaItemImpl::isSelected() const
{
    return m_selected;
}

bool MediaItemImpl::isChecked() const
{
    return m_checked;
}

const MediaItem::Metadata & MediaItemImpl::metadata() const
{
    return m_metadata;
}

MediaItemImpl * MediaItemImpl::impl()
{
    return this;
}

/*QString MediaItemImpl::getFolder() const
{
    if(!m_filename.startsWith(m_basePath + "/")) {
        return QString();
    }
    QString tmp = m_filename.mid(m_basePath.length() + 1);
    int p = tmp.indexOf(QChar('/'));
    if(p < 0) {
        return QString();
    }
    tmp = tmp.left(p);
    if(tmp.length() == 4 && tmp[0].isDigit() && tmp[1].isDigit() && tmp[2].isDigit() && tmp[3].isDigit())
        return QString();
    return tmp;
}
*/
